package edu.ccut.saturn.studio.logic.parts.policy;

import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gef.requests.LocationRequest;
import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import edu.ccut.saturn.studio.logic.parts.ShapeEditPart;

public class SaturnTipParameterEditPolicy extends AbstractEditPolicy {
	public static final String TOOLTIP_ROLE = "tooltip_role";
	private DefaultInformationControl informationControl;

	public SaturnTipParameterEditPolicy() {
		informationControl = null;
	}

	public void eraseTargetFeedback(Request request) {
		if (request.getType().equals("selection hover"))
			hideToolTip();
		else
			super.eraseTargetFeedback(request);
	}

	public void showTargetFeedback(Request request) {
		if (request.getType().equals("selection hover")) {
			if (request instanceof LocationRequest) {
				Point point = new Point(((LocationRequest) request)
						.getLocation().x, ((LocationRequest) request)
						.getLocation().y);
				showToolTip(point);
			}
		} else {
			super.showTargetFeedback(request);
		}
	}

	private void showToolTip(Point point) {
		if (getGraphicalNodeEditPart().isShowTipOnTop())
			return;
		String s = getGraphicalNodeEditPart().getToolTipContent();
		if (s == null)
			return;
		String s1 = null;
		if (getHost().getViewer().getSelectedEditParts().contains(getHost()))
			s1 = "Press 'F2' for focus";
		if (informationControl == null) {
			informationControl = new DefaultInformationControl(null, 0,
					new HTMLTextPresenter(), s1);
			informationControl.setSizeConstraints(2147483647, 2147483647);
			informationControl.setInformation(s);
			Point point1 = getHost().getViewer().getControl().toDisplay(point);
			setHoverLocation(informationControl, point1, informationControl
					.computeSizeHint(), 26);
			informationControl.setVisible(true);
		}
	}

	protected ShapeEditPart getGraphicalNodeEditPart() {
		return (ShapeEditPart) getHost();
	}

	private void hideToolTip() {
		if (informationControl != null) {
			informationControl.setVisible(false);
			informationControl.dispose();
			informationControl = null;
		}
	}

	private void setHoverLocation(
			DefaultInformationControl defaultinformationcontrol, Point point,
			Point point1, int i) {
		Display display = getHost().getViewer().getControl().getDisplay();
		Rectangle rectangle = display.getBounds();
		point1.x = Math.min(500, point1.x);
		point1.y = Math.min(350, point1.y);
		if (point.x + point1.x > rectangle.width)
			point.x = rectangle.width - point1.x;
		if (point.y + point1.y + i > rectangle.height)
			point.y = point.y - point1.y - i - 16;
		defaultinformationcontrol.setLocation(new Point(point.x, point.y + i));
		defaultinformationcontrol.setSize(point1.x, point1.y);
	}

}
